package com.effectivcrm;

import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.extras.tiles2.dialect.TilesDialect;
import org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer;
import org.thymeleaf.extras.tiles2.spring4.web.view.ThymeleafTilesView;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;

import com.effectivcrm.startup.ModelEnhancingInterceptor;
import com.effectivcrm.thymeleaf.extension.ValidatorDialect;

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {

	@Bean
	public TemplateResolver getTemplateResolver() {
		ClassLoaderTemplateResolver resolver = BeanUtils
				.instantiate(ClassLoaderTemplateResolver.class);
		resolver.setPrefix("templates/");
		resolver.setSuffix(".html");
		resolver.setTemplateMode("HTML5");
		resolver.setOrder(1);
		return resolver;
	}

	@Bean
	public ViewResolver getTilesViewResolver() {
		ThymeleafViewResolver viewResolver = BeanUtils
				.instantiate(ThymeleafViewResolver.class);
		viewResolver.setTemplateEngine(getTemplateEngine());

		viewResolver.setViewClass(ThymeleafTilesView.class);
		viewResolver.setExcludedViewNames(new String[] { "webjars/*" });
		viewResolver.setOrder(Ordered.HIGHEST_PRECEDENCE);

		return viewResolver;
	}

	@Bean
	public SpringTemplateEngine getTemplateEngine() {
		SpringTemplateEngine templateEngine = BeanUtils
				.instantiate(SpringTemplateEngine.class);
		templateEngine.setTemplateResolver(getTemplateResolver());
		templateEngine.addDialect(BeanUtils.instantiate(TilesDialect.class));
		templateEngine.addDialect(BeanUtils.instantiate(ValidatorDialect.class));	
		return templateEngine;
	}

	@Bean
	public ThymeleafTilesConfigurer tilesConfigurer() {
		ThymeleafTilesConfigurer ttc = BeanUtils
				.instantiate(ThymeleafTilesConfigurer.class);
		// ttc.setDefinitions(new
		// String[]{"/WEB-INF/views/message/tiles-defs.xml"});
		return ttc;
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {

		if (!registry.hasMappingForPattern("/webjars/**")) {
			registry.addResourceHandler("/webjars/**").addResourceLocations(
					"classpath:/META-INF/resources/webjars/");
		}
	}

	@Bean
	public HandlerInterceptor getInterceptor() {
		ModelEnhancingInterceptor interceptor = BeanUtils
				.instantiate(ModelEnhancingInterceptor.class);
		return interceptor;
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {

		registry.addInterceptor(getInterceptor());
	}

}
